import {
  productList,
  projectList,
  productTree,
  PCFrequestList,
  materialList,
  selfMadeEmissionList,
  purposeScopeList
} from "@/assets/mock/mockData";
import { Product, ProductTree, Project, PCFrequest, Material, SelfMadeEmission, PurposeScope } from "@/api/interface";

/**从缓存或mock中获取产品列表信息 */
const getProductListData = (): Partial<Product.ResProduct>[] => {
  const key = "productList";
  const savedProducts = localStorage.getItem(key);

  if (savedProducts) {
    try {
      const productsData = JSON.parse(savedProducts);
      console.log("从缓存中读取的产品列表:", productsData);
      return productsData;
    } catch (error) {
      console.error("解析缓存数据时出错:", error);
      localStorage.setItem(key, JSON.stringify(productList.value));
      console.log("缓存数据无效，已重新写入默认产品列表:", productList.value);
      return productList.value;
    }
  } else {
    localStorage.setItem(key, JSON.stringify(productList.value));
    console.log("缓存中没有找到产品列表，已写入默认产品列表:", productList.value);
    return productList.value;
  }
};
/**将传入的产品列表信息写入缓存 */
const setProductListData = (products: Partial<Product.ResProduct>[]): void => {
  const key = "productList";
  try {
    localStorage.setItem(key, JSON.stringify(products));
    productList.value = products;
    console.log("产品列表已成功写入缓存:6", products);
  } catch (error) {
    console.error("写入缓存时出错:", error);
  }
};

/**从缓存或mock中获取产品结构树信息 */
const getProductTreeData = (): Partial<ProductTree.ResProduct_Tree>[] => {
  const key = "productTree";
  const savedProductTree = localStorage.getItem(key);

  if (savedProductTree) {
    try {
      const productTreeData = JSON.parse(savedProductTree);
      console.log("从缓存中读取的产品结构树:", productTreeData);
      return productTreeData;
    } catch (error) {
      console.error("解析缓存数据时出错:", error);
      localStorage.setItem(key, JSON.stringify(productTree.value));
      console.log("缓存数据无效，已重新写入默认产品结构树:", productTree.value);
      return productTree.value;
    }
  } else {
    localStorage.setItem(key, JSON.stringify(productTree.value));
    console.log("缓存中没有找到产品结构树，已写入默认产品结构树:", productTree.value);
    return productTree.value;
  }
};
/**将传入的产品结构树信息写入缓存 */
const setProductTreeData = (trees: Partial<ProductTree.ResProduct_Tree>[]): void => {
  const key = "productTree";
  try {
    localStorage.setItem(key, JSON.stringify(trees));
    productTree.value = trees;
    console.log("产品结构树已成功写入缓存:", trees);
    console.log("产品结构树:", productTree.value);
  } catch (error) {
    console.error("写入缓存时出错:", error);
  }
};

/**从缓存或mock中获取项目列表信息 */
const getProjectData = (): Partial<Project.ResProject>[] => {
  const key = "projectList";
  const savedProjects = localStorage.getItem(key);

  if (savedProjects) {
    try {
      const projectsData = JSON.parse(savedProjects);
      console.log("从缓存中读取的项目列表:", projectsData);
      return projectsData;
    } catch (error) {
      console.error("解析缓存数据时出错:", error);
      localStorage.setItem(key, JSON.stringify(projectList.value));
      console.log("缓存数据无效，已重新写入默认项目列表:", projectList.value);
      return projectList.value;
    }
  } else {
    localStorage.setItem(key, JSON.stringify(projectList.value));
    console.log("缓存中没有找到项目列表，已写入默认项目列表:", projectList.value);
    return projectList.value;
  }
};

/**将传入的项目列表信息写入缓存 */
const setProjectData = (projects: Partial<Project.ResProject>[]): void => {
  const projectKey = "projectList";
  try {
    localStorage.setItem(projectKey, JSON.stringify(projects));
    projectList.value = projects;
    console.log("项目列表已成功写入缓存:", projects);
  } catch (error) {
    console.error("写入缓存时出错:", error);
  }
};

/**从缓存或mock中获取PCF请求列表信息 */
const getPCFrequestListData = (): Partial<PCFrequest.ResPCFrequest>[] => {
  const key = "PCFrequestList";
  const savedPCFrequests = localStorage.getItem(key);

  if (savedPCFrequests) {
    try {
      const pcfRequestsData = JSON.parse(savedPCFrequests);
      console.log("从缓存中读取的PCF请求列表:", pcfRequestsData);
      return pcfRequestsData;
    } catch (error) {
      console.error("解析缓存数据时出错:", error);
      localStorage.setItem(key, JSON.stringify(PCFrequestList.value));
      console.log("缓存数据无效，已重新写入默认PCF请求列表:", PCFrequestList.value);
      return PCFrequestList.value;
    }
  } else {
    localStorage.setItem(key, JSON.stringify(PCFrequestList.value));
    console.log("缓存中没有找到PCF请求列表，已写入默认PCF请求列表:", PCFrequestList.value);
    return PCFrequestList.value;
  }
};
/**将传入的PCF请求列表信息写入缓存 */
const setPCFrequestListData = (pcfRequests: Partial<PCFrequest.ResPCFrequest>[]): void => {
  const key = "PCFrequestList";
  try {
    localStorage.setItem(key, JSON.stringify(pcfRequests));
    PCFrequestList.value = pcfRequests;
    console.log("PCF请求列表已成功写入缓存:", pcfRequests);
  } catch (error) {
    console.error("写入缓存时出错:", error);
  }
};

/**从缓存或mock中获取物料列表信息 */
const getMaterialListData = (): Partial<Material.ResMaterial>[] => {
  const key = "materialList";
  const savedMaterials = localStorage.getItem(key);

  if (savedMaterials) {
    try {
      const materialsData = JSON.parse(savedMaterials);
      console.log("从缓存中读取的物料列表:", materialsData);
      return materialsData;
    } catch (error) {
      console.error("解析缓存数据时出错:", error);
      localStorage.setItem(key, JSON.stringify(materialList.value));
      console.log("缓存数据无效，已重新写入默认物料列表:", materialList.value);
      return materialList.value;
    }
  } else {
    localStorage.setItem(key, JSON.stringify(materialList.value));
    console.log("缓存中没有找到物料列表，已写入默认物料列表:", materialList.value);
    return materialList.value;
  }
};

/**将传入的物料列表信息写入缓存 */
const setMaterialListData = (materials: Partial<Material.ResMaterial>[]): void => {
  const key = "materialList";
  try {
    localStorage.setItem(key, JSON.stringify(materials));
    materialList.value = materials;
    console.log("物料列表已成功写入缓存:", materials);
  } catch (error) {
    console.error("写入缓存时出错:", error);
  }
};

/**从缓存或mock中获取自制物料碳排放源分析数据 */
const getSelfMadeEmissionData = (projectId: string): Partial<SelfMadeEmission.ResSelfMadeEmission>[] => {
  const key = `selfMadeData_${projectId}`;
  const savedData = localStorage.getItem(key);

  if (savedData) {
    try {
      const emissionData = JSON.parse(savedData);
      console.log("从缓存中读取的自制物料碳排放源分析数据:", emissionData);
      return emissionData;
    } catch (error) {
      console.error("解析缓存数据时出错:", error);
    }
  }

  // 从 list 中找到对应项目的数据
  const projectData = selfMadeEmissionList.value.filter(item => item.projectId === projectId);
  if (projectData.length > 0) {
    localStorage.setItem(key, JSON.stringify(projectData));
    console.log("从列表中获取数据并写入缓存:", projectData);
    return projectData;
  }

  // 如果缓存和列表都没有数据，返回空数组
  return [];
};

/**将传入的自制物料碳排放源分析数据写入缓存 */
// const setSelfMadeEmissionData = (projectId: string, data: Partial<SelfMadeEmission.ResSelfMadeEmission>[]): void => {
//   const key = `selfMadeData_${projectId}`;
//   try {
//     localStorage.setItem(key, JSON.stringify(data));
//     // 更新对应项目的数据
//     const filteredList = selfMadeEmissionList.value.filter(item => item.projectId !== projectId);
//     selfMadeEmissionList.value = [...filteredList, ...data];
//     console.log("自制物料碳排放源分析数据已成功写入缓存:", data);
//   } catch (error) {
//     console.error("写入缓存时出错:", error);
//   }
// };

/**从缓存或mock中获取目标与范围数据 */
const getPurposeScopeData = (projectId: string): Partial<PurposeScope.PurposeScopeData> | undefined => {
  const key = `purposeScopeData_${projectId}`;
  const savedData = localStorage.getItem(key);

  if (savedData) {
    try {
      const purposeScopeData = JSON.parse(savedData);
      console.log("从缓存中读取的目标与范围数据:", purposeScopeData);
      return purposeScopeData;
    } catch (error) {
      console.error("解析缓存数据时出错:", error);
      // 从 list 中找到对应项目的数据
      const projectData = purposeScopeList.value.find(item => item.projectId === projectId);
      if (projectData) {
        localStorage.setItem(key, JSON.stringify(projectData));
        console.log("缓存数据无效，已重新写入项目数据:", projectData);
      }
      return projectData;
    }
  }

  // 如果没有缓存数据，从 purposeScopeList 获取
  const purposeScopeData = purposeScopeList.value.find(item => item.projectId === projectId);
  if (purposeScopeData) {
    localStorage.setItem(key, JSON.stringify(purposeScopeData));
    console.log("缓存中没有找到数据，已写入项目数据:", purposeScopeData);
  }
  return purposeScopeData;
};

export {
  getProductListData,
  setProductListData,
  getProductTreeData,
  setProductTreeData,
  getProjectData,
  setProjectData,
  getPCFrequestListData,
  setPCFrequestListData,
  getMaterialListData,
  setMaterialListData,
  getSelfMadeEmissionData,
  setSelfMadeEmissionData,
  getPurposeScopeData
};
